using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using Indeco.G24.Entities;
using Indeco.G24.Services;

/// <summary>
/// Summary description for Global
/// </summary>
namespace Indeco.G24.Web
{
    /// <summary>
    /// Summary description for Global.
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
        //DMS.DataService.User usersession = new DMS.DataService.User();
        NguoiDung nguoiDung = new NguoiDung();
        public const string SESSION_KEY_CULTURE = "culture";
        public const string SESSION_KEY_TEXT = "English";
        /// <summary>
        /// 
        /// </summary>
        public Global()
        {
            InitializeComponent();
        }

        protected void Application_Start(Object sender, EventArgs e)
        {
            Application["UserOnline"] = 0;
        }

        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            // Security fix: http://support.microsoft.com/?kbid=887459
            if (Request.Path.IndexOf('\\') >= 0 ||
                System.IO.Path.GetFullPath(Request.PhysicalPath) != Request.PhysicalPath)
            {
                throw new HttpException(404, "not found");
            }
        }

        protected void Application_EndRequest(Object sender, EventArgs e)
        {

        }

        public void Application_AuthenticateRequest(Object s, EventArgs e)
        {
            //if (HttpContext.Current.User != null)
            //{
            //    if (HttpContext.Current.User.Identity.AuthenticationType == "Forms")
            //    {
            //        System.Web.Security.FormsIdentity id = HttpContext.Current.User.Identity;
            //        String[] myRoles = new String[3];
            //        myRoles[0] = "managers";
            //        myRoles[1] = "testers";
            //        myRoles[2] = "developers";
            //        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, myRoles);
            //    }
            //}
        }

        protected void Application_Error(Object sender, EventArgs e)
        {
            Exception currentError = Server.GetLastError();
            string message = "";

            if (currentError is HttpException)
            {
                if (((HttpException)currentError).GetHttpCode() == 404)
                    message = @"
                                <-><-><-><-><-><-><-><-><-><-><-><->
                                Request URL: " + HttpContext.Current.Request.Url.ToString() + @"
                                ------------------------------------
                                404 Error - file not found
                                <-><-><-><-><-><-><-><-><-><-><-><->

                                ";
            }

            if (message.Length == 0)
            {
                message = @"
                    <-><-><-><-><-><-><-><-><-><-><-><->
                    Request URL: " + HttpContext.Current.Request.Url.ToString() + @"
                    ------------------------------------
                    Fields: " + this.Server.UrlDecode(HttpContext.Current.Request.Params.ToString()) + @"
                    ------------------------------------
                    Error: " + currentError.ToString() + @"
                    <-><-><-><-><-><-><-><-><-><-><-><->

                    ";
            }


            // Write error to log file
            //DMS.DataService.AppException.LogError(message);

            // Write error to screen
            // TODO: scrub what we show to end user!
            //DMS.Web.DmsPage.ShowError(currentError);
            // Clear error so that it does not bubble up

        }

        protected void Session_Start(Object sender, EventArgs e)
        {
            //Business.User userid = new Business.User(((SiteIdentity)Context.User.Identity).UserID);
            //set english as default startup language
            Session[Global.SESSION_KEY_CULTURE] = Culture.VN;  
            Session["G24SessionId"] = Guid.NewGuid();
            Session["IDNhaCungCap"] = Guid.Empty;
            Session["IDNguoiDung"] = Guid.Empty;
            Session["IDDiaBan"] = Guid.Empty;
            Application["UserOnline"] = Convert.ToInt32(Application["UserOnline"]) + 1;
            try
            {
                if (System.Web.Security.Membership.GetUser().UserName != "")
                {
                    NguoiDungService nguoiDungService = new NguoiDungService();
                    TList<NguoiDung> nguoiDung = nguoiDungService.Find("TenSuDung = '" + System.Web.Security.Membership.GetUser().UserName + "'");
                    Session["IDNguoiDung"] = nguoiDung[0].IDNguoiDung;
                    NguoiDungNhaCungCapService nguoiDungNhaCungCapService = new NguoiDungNhaCungCapService();
                    TList<NguoiDungNhaCungCap> listNN = nguoiDungNhaCungCapService.GetByIDNguoiDung(new Guid(Session["IDNguoiDung"].ToString()));
                    Session["IDNhaCungCap"] = listNN[0].IDNhaCungCap;
                }
            }
            catch
            { }
        }
        protected void Session_End(Object sender, EventArgs e)
        {
            //DMSPrincipal cachedNewUser = (DMSPrincipal)Session["UserInfo"];
            //Business.User user = new Business.User(cachedNewUser);
            //DMS.DataService.User u = new DMS.DataService.User();
            //int userid = user.UserID;
            //bool insert = u.InsertLog(userid, "Logout");
            //bool updatestatus = u.UpdateStatus(Convert.ToInt32(userid), "Logout");
            ////if (insert && updatestatus)
            ////{
            ////    Response.Redirect("Login.aspx");
            ////}    
            //G24Principal cachedNewUser = (G24Principal)c;
           // NguoiDung nguoiDung = new Business.User(cachedNewUser);
            Application["UserOnline"] = Convert.ToInt32(Application["UserOnline"]) - 1;


        }

        protected void Application_End(Object sender, EventArgs e)
        {

        }

        #region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion
    }
}
